{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "“austen-emma.txt”的文本长度为887071,词汇数量为192427，句子数量为7752\n",
      "“austen-persuasion.txt”的文本长度为466292,词汇数量为98171，句子数量为3747\n",
      "“austen-sense.txt”的文本长度为673022,词汇数量为141576，句子数量为4999\n",
      "“bible-kjv.txt”的文本长度为4332554,词汇数量为1010654，句子数量为30103\n",
      "“blake-poems.txt”的文本长度为38153,词汇数量为8354，句子数量为438\n",
      "“bryant-stories.txt”的文本长度为249439,词汇数量为55563，句子数量为2863\n",
      "“burgess-busterbrown.txt”的文本长度为84663,词汇数量为18963，句子数量为1054\n",
      "“carroll-alice.txt”的文本长度为144395,词汇数量为34110，句子数量为1703\n",
      "“chesterton-ball.txt”的文本长度为457450,词汇数量为96996，句子数量为4779\n",
      "“chesterton-brown.txt”的文本长度为406629,词汇数量为86063，句子数量为3806\n",
      "“chesterton-thursday.txt”的文本长度为320525,词汇数量为69213，句子数量为3742\n",
      "“edgeworth-parents.txt”的文本长度为935158,词汇数量为210663，句子数量为10230\n",
      "“melville-moby_dick.txt”的文本长度为1242990,词汇数量为260819，句子数量为10059\n",
      "“milton-paradise.txt”的文本长度为468220,词汇数量为96825，句子数量为1851\n",
      "“shakespeare-caesar.txt”的文本长度为112310,词汇数量为25833，句子数量为2163\n",
      "“shakespeare-hamlet.txt”的文本长度为162881,词汇数量为37360，句子数量为3106\n",
      "“shakespeare-macbeth.txt”的文本长度为100351,词汇数量为23140，句子数量为1907\n",
      "“whitman-leaves.txt”的文本长度为711215,词汇数量为154883，句子数量为4250\n"
     ]
    }
   ],
   "source": [
    "from nltk.corpus import gutenberg\n",
    "for fileid in gutenberg.fileids():\n",
    "    raw = gutenberg.raw(fileid)\n",
    "    num_length = len(raw)\n",
    "    words = gutenberg.words(fileid)\n",
    "    num_words = len(words)\n",
    "    sents = gutenberg.sents(fileid)\n",
    "    num_sents = len(sents)\n",
    "    print(\"“%s”的文本长度为%d,词汇数量为%d，句子数量为%d\"%(fileid,num_length,num_words,num_sents))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "布朗语料库的类别:\n",
      "['adventure', 'belles_lettres', 'editorial', 'fiction', 'government', 'hobbies', 'humor', 'learned', 'lore', 'mystery', 'news', 'religion', 'reviews', 'romance', 'science_fiction']\n",
      "布朗语料库“news”类别中的文件:\n",
      "['ca01', 'ca02', 'ca03', 'ca04', 'ca05', 'ca06', 'ca07', 'ca08', 'ca09', 'ca10', 'ca11', 'ca12', 'ca13', 'ca14', 'ca15', 'ca16', 'ca17', 'ca18', 'ca19', 'ca20', 'ca21', 'ca22', 'ca23', 'ca24', 'ca25', 'ca26', 'ca27', 'ca28', 'ca29', 'ca30', 'ca31', 'ca32', 'ca33', 'ca34', 'ca35', 'ca36', 'ca37', 'ca38', 'ca39', 'ca40', 'ca41', 'ca42', 'ca43', 'ca44']\n",
      "布朗语料库“news”类别中的词汇:\n",
      "['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', ...]\n",
      "布朗语料库“news”类别中的句子:\n",
      "[['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', \"Atlanta's\", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', \"''\", 'that', 'any', 'irregularities', 'took', 'place', '.'], ['The', 'jury', 'further', 'said', 'in', 'term-end', 'presentments', 'that', 'the', 'City', 'Executive', 'Committee', ',', 'which', 'had', 'over-all', 'charge', 'of', 'the', 'election', ',', '``', 'deserves', 'the', 'praise', 'and', 'thanks', 'of', 'the', 'City', 'of', 'Atlanta', \"''\", 'for', 'the', 'manner', 'in', 'which', 'the', 'election', 'was', 'conducted', '.'], ...]\n"
     ]
    }
   ],
   "source": [
    "from nltk.corpus import brown\n",
    "print(\"布朗语料库的类别:\")\n",
    "print(brown.categories())\n",
    "print(\"布朗语料库“news”类别中的文件:\")\n",
    "print(brown.fileids(categories='news'))\n",
    "print(\"布朗语料库“news”类别中的词汇:\")\n",
    "print(brown.words(categories='news'))\n",
    "print(\"布朗语料库“news”类别中的句子:\")\n",
    "print(brown.sents(categories='news'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路透社语料库中前5个文件:\n",
      "['test/14826', 'test/14828', 'test/14829', 'test/14832', 'test/14833']\n",
      "路透社语料库的类别:\n",
      "['acq', 'alum', 'barley', 'bop', 'carcass', 'castor-oil', 'cocoa', 'coconut', 'coconut-oil', 'coffee', 'copper', 'copra-cake', 'corn', 'cotton', 'cotton-oil', 'cpi', 'cpu', 'crude', 'dfl', 'dlr', 'dmk', 'earn', 'fuel', 'gas', 'gnp', 'gold', 'grain', 'groundnut', 'groundnut-oil', 'heat', 'hog', 'housing', 'income', 'instal-debt', 'interest', 'ipi', 'iron-steel', 'jet', 'jobs', 'l-cattle', 'lead', 'lei', 'lin-oil', 'livestock', 'lumber', 'meal-feed', 'money-fx', 'money-supply', 'naphtha', 'nat-gas', 'nickel', 'nkr', 'nzdlr', 'oat', 'oilseed', 'orange', 'palladium', 'palm-oil', 'palmkernel', 'pet-chem', 'platinum', 'potato', 'propane', 'rand', 'rape-oil', 'rapeseed', 'reserves', 'retail', 'rice', 'rubber', 'rye', 'ship', 'silver', 'sorghum', 'soy-meal', 'soy-oil', 'soybean', 'strategic-metal', 'sugar', 'sun-meal', 'sun-oil', 'sunseed', 'tea', 'tin', 'trade', 'veg-oil', 'wheat', 'wpi', 'yen', 'zinc']\n",
      "文件“test/14828”的所属类别:['grain']\n",
      "类别“crude”和\"cron\"对应的文件:\n",
      "['test/14829', 'test/15063', 'test/15200', 'test/15230', 'test/15238', 'test/15244', 'test/15322', 'test/15339', 'test/15344', 'test/15351', 'test/15520', 'test/15939', 'test/15964', 'test/16005', 'test/16007', 'test/16040', 'test/16077', 'test/16366', 'test/16429', 'test/16438', 'test/16593', 'test/16607', 'test/16636', 'test/16649', 'test/16651', 'test/16658', 'test/16710', 'test/16723', 'test/16739', 'test/16762', 'test/17054', 'test/17478', 'test/17516', 'test/17519', 'test/17584', 'test/17618', 'test/17658', 'test/17669', 'test/17750', 'test/17757', 'test/17771', 'test/17780', 'test/17813', 'test/17816', 'test/17875', 'test/17886', 'test/17888', 'test/17892', 'test/17913', 'test/17929', 'test/17963', 'test/18066', 'test/18085', 'test/18108', 'test/18146', 'test/18186', 'test/18213', 'test/18234', 'test/18311', 'test/18325', 'test/18329', 'test/18332', 'test/18340', 'test/18493', 'test/18521', 'test/18523', 'test/18621', 'test/18651', 'test/18655', 'test/18678', 'test/18680', 'test/18689', 'test/18692', 'test/18698', 'test/18701', 'test/18704', 'test/18705', 'test/18706', 'test/18707', 'test/18728', 'test/18736', 'test/18738', 'test/18743', 'test/18746', 'test/18747', 'test/18754', 'test/18765', 'test/18773', 'test/18774', 'test/18789', 'test/18795', 'test/18810', 'test/18824', 'test/18840', 'test/18857', 'test/18896', 'test/19069', 'test/19110', 'test/19128', 'test/19182', 'test/19193', 'test/19285', 'test/19291', 'test/19397', 'test/19403', 'test/19492', 'test/19497', 'test/19499', 'test/19505', 'test/19506', 'test/19509', 'test/19556', 'test/19559', 'test/19560', 'test/19684', 'test/19756', 'test/19832', 'test/19844', 'test/19869', 'test/19903', 'test/19927', 'test/19996', 'test/19998', 'test/20008', 'test/20030', 'test/20090', 'test/20092', 'test/20093', 'test/20095', 'test/20101', 'test/20103', 'test/20270', 'test/20333', 'test/20420', 'test/20459', 'test/20464', 'test/20474', 'test/20632', 'test/20653', 'test/20662', 'test/20692', 'test/20709', 'test/20721', 'test/20730', 'test/20756', 'test/20774', 'test/20778', 'test/20828', 'test/20869', 'test/20878', 'test/20881', 'test/20882', 'test/20890', 'test/20909', 'test/20919', 'test/20936', 'test/20944', 'test/20959', 'test/20981', 'test/20991', 'test/21002', 'test/21006', 'test/21013', 'test/21018', 'test/21058', 'test/21067', 'test/21076', 'test/21131', 'test/21149', 'test/21197', 'test/21216', 'test/21267', 'test/21274', 'test/21363', 'test/21369', 'test/21380', 'test/21417', 'test/21443', 'test/21459', 'test/21465', 'test/21475', 'test/21482', 'test/21484', 'test/21485', 'test/21492', 'test/21502', 'test/21506', 'test/21541', 'test/21568', 'training/10011', 'training/10078', 'training/10080', 'training/10106', 'training/10168', 'training/10190', 'training/10192', 'training/10200', 'training/10228', 'training/1026', 'training/10268', 'training/10395', 'training/10406', 'training/10452', 'training/10539', 'training/10567', 'training/10588', 'training/10620', 'training/10621', 'training/10627', 'training/10632', 'training/10641', 'training/10669', 'training/10693', 'training/10750', 'training/10797', 'training/10845', 'training/10873', 'training/10947', 'training/11007', 'training/11025', 'training/11100', 'training/11118', 'training/11149', 'training/11213', 'training/11231', 'training/11350', 'training/11388', 'training/11403', 'training/11421', 'training/11444', 'training/11455', 'training/11491', 'training/11559', 'training/11632', 'training/11639', 'training/11699', 'training/11723', 'training/11731', 'training/12267', 'training/12286', 'training/12371', 'training/12503', 'training/12533', 'training/127', 'training/12775', 'training/12803', 'training/12940', 'training/1306', 'training/13096', 'training/13102', 'training/13115', 'training/13142', 'training/13184', 'training/13200', 'training/1324', 'training/13256', 'training/13266', 'training/1331', 'training/1335', 'training/1343', 'training/13542', 'training/13611', 'training/13633', 'training/1387', 'training/13963', 'training/14211', 'training/14395', 'training/144', 'training/14698', 'training/14704', 'training/14709', 'training/14732', 'training/1521', 'training/1556', 'training/1616', 'training/1661', 'training/1686', 'training/1692', 'training/1709', 'training/1711', 'training/1799', 'training/1851', 'training/1856', 'training/1875', 'training/1878', 'training/189', 'training/1904', 'training/1909', 'training/191', 'training/194', 'training/1948', 'training/1980', 'training/1990', 'training/2004', 'training/2007', 'training/2046', 'training/2061', 'training/211', 'training/2121', 'training/2174', 'training/2175', 'training/2187', 'training/2231', 'training/236', 'training/237', 'training/2383', 'training/242', 'training/2449', 'training/246', 'training/248', 'training/2511', 'training/2515', 'training/2517', 'training/2522', 'training/2530', 'training/2585', 'training/2688', 'training/273', 'training/2767', 'training/2775', 'training/2838', 'training/2925', 'training/2957', 'training/2970', 'training/2973', 'training/2998', 'training/3003', 'training/3015', 'training/3017', 'training/3048', 'training/3065', 'training/3115', 'training/3145', 'training/3146', 'training/3162', 'training/3169', 'training/3174', 'training/3189', 'training/3204', 'training/3303', 'training/3332', 'training/3342', 'training/3354', 'training/3364', 'training/3389', 'training/3430', 'training/3452', 'training/3455', 'training/349', 'training/3507', 'training/3509', 'training/352', 'training/353', 'training/3556', 'training/3563', 'training/3571', 'training/3592', 'training/3594', 'training/3609', 'training/368', 'training/3798', 'training/3843', 'training/3869', 'training/3906', 'training/3959', 'training/3976', 'training/3980', 'training/3985', 'training/3995', 'training/4016', 'training/4028', 'training/4039', 'training/4041', 'training/4125', 'training/4129', 'training/4162', 'training/4171', 'training/4174', 'training/4232', 'training/4246', 'training/4315', 'training/4333', 'training/4340', 'training/4365', 'training/4367', 'training/4386', 'training/4425', 'training/4429', 'training/4445', 'training/4453', 'training/4462', 'training/4466', 'training/4467', 'training/4474', 'training/4481', 'training/4525', 'training/4558', 'training/4569', 'training/4576', 'training/4578', 'training/4584', 'training/459', 'training/4590', 'training/4593', 'training/4600', 'training/4604', 'training/4609', 'training/4658', 'training/4662', 'training/4664', 'training/4689', 'training/4713', 'training/4714', 'training/4742', 'training/4831', 'training/4848', 'training/4867', 'training/489', 'training/4951', 'training/4953', 'training/4962', 'training/4983', 'training/502', 'training/5037', 'training/5061', 'training/511', 'training/5116', 'training/5118', 'training/5119', 'training/5123', 'training/5125', 'training/5150', 'training/5156', 'training/5166', 'training/5167', 'training/5171', 'training/5244', 'training/5268', 'training/5270', 'training/5273', 'training/5281', 'training/543', 'training/5542', 'training/5553', 'training/5630', 'training/5683', 'training/5796', 'training/5852', 'training/5866', 'training/6023', 'training/6060', 'training/6086', 'training/6111', 'training/6119', 'training/6125', 'training/6159', 'training/6163', 'training/6166', 'training/6169', 'training/6177', 'training/6184', 'training/6201', 'training/6208', 'training/6264', 'training/6294', 'training/6301', 'training/6348', 'training/6404', 'training/6413', 'training/6432', 'training/6578', 'training/6598', 'training/6606', 'training/6652', 'training/6656', 'training/6722', 'training/6742', 'training/6746', 'training/6760', 'training/6791', 'training/6871', 'training/6876', 'training/6893', 'training/6905', 'training/6913', 'training/6996', 'training/704', 'training/7067', 'training/708', 'training/7097', 'training/7150', 'training/7152', 'training/7174', 'training/7287', 'training/7355', 'training/7423', 'training/7496', 'training/7528', 'training/7529', 'training/7611', 'training/7618', 'training/7639', 'training/7684', 'training/7742', 'training/7854', 'training/791', 'training/8015', 'training/8041', 'training/8069', 'training/8089', 'training/8100', 'training/8117', 'training/8131', 'training/8134', 'training/8160', 'training/8167', 'training/8188', 'training/8209', 'training/8210', 'training/8288', 'training/835', 'training/8402', 'training/8405', 'training/8421', 'training/8440', 'training/8478', 'training/8493', 'training/8516', 'training/8553', 'training/8598', 'training/8600', 'training/8606', 'training/8610', 'training/8623', 'training/8630', 'training/8672', 'training/8675', 'training/873', 'training/8749', 'training/8755', 'training/8765', 'training/8812', 'training/8815', 'training/8820', 'training/8835', 'training/8856', 'training/8882', 'training/8914', 'training/8959', 'training/8964', 'training/8971', 'training/9031', 'training/9039', 'training/9065', 'training/9155', 'training/918', 'training/9208', 'training/9253', 'training/9279', 'training/9293', 'training/930', 'training/9436', 'training/9445', 'training/945', 'training/952', 'training/9527', 'training/9583', 'training/9614', 'training/9634', 'training/9639', 'training/9650', 'training/9674', 'training/9718', 'training/9801', 'training/9849', 'training/988', 'training/9913', 'training/9947']\n"
     ]
    }
   ],
   "source": [
    "from nltk.corpus import reuters\n",
    "print('路透社语料库中前5个文件:')\n",
    "print(reuters.fileids()[:5])\n",
    "print('路透社语料库的类别:')\n",
    "print(reuters.categories())\n",
    "print('文件“test/14828”的所属类别:%s'%(reuters.categories(\"test/14828\")))\n",
    "print('类别“crude”和\"cron\"对应的文件:')\n",
    "print(reuters.fileids([\"crude\",\"cron\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "第一回     灵根育孕源流出　心性修持大道生\r\n",
      "\r\n",
      "\r\n",
      "　　诗曰：\r\n",
      "　　　　混沌未分天地乱，茫茫渺渺无人见。\r\n",
      "　　　　自从盘古破鸿蒙，开辟从兹清浊辨。\r\n",
      "　　　　覆载群生仰至仁，发明万物皆成善。\r\n",
      "　　　　欲知造化会元功，须看西游释厄传。\r\n",
      "\r\n",
      "\r\n",
      "盖闻天地之数，有十二万九千六百岁为一元。将一元分为十二会，乃子、丑、寅\r\n",
      "、卯、辰、巳、午、未、申、酉、戌、亥之十二支也。每会该一万八百岁。且就\r\n",
      "一日而论：子时得阳气，而丑则鸡鸣﹔寅不通光，而卯则日出﹔辰时食后，而巳\r\n",
      "则挨排﹔日午天中，而未则西蹉﹔申时晡，而日落酉，戌黄昏，而人定亥。譬于\r\n",
      "大数，若到戌会之终，则天地昏曚而万物否矣。再去五千四百岁，交亥会之初，\r\n",
      "则当黑暗，而两间人物俱无矣，故曰混沌。又五千四百岁，亥会将终，贞下起元\r\n",
      "，近子之会，而复逐渐开明。邵康节曰：：“冬至子之半，天心无改移。一阳初\r\n",
      "动处，万物未生时。”到\n"
     ]
    }
   ],
   "source": [
    "from urllib.request import urlopen\n",
    "from zhconv import convert\n",
    "url = 'https://www.gutenberg.org/files/23962/23962-0.txt'\n",
    "html = urlopen(url).read()\n",
    "html = html.decode('utf-8')\n",
    "html = convert(html[600:1000], 'zh-hans')\n",
    "print(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "句子“ 电脑是20世纪人类最伟大的发明之一 ”是以“电脑”开头的\n",
      "将‘电脑’替换为‘计算机’后的结果:  计算机是20世纪人类最伟大的发明之一\n",
      "句子“ 按性能的不同，电脑可分为巨型机、大型机、小型机、工作站和微型电脑等 ”不是以“电脑”开头的\n",
      "将‘电脑’替换为‘计算机’后的结果:  按性能的不同，计算机可分为巨型机、大型机、小型机、工作站和微型计算机等\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "text = '电脑是20世纪人类最伟大的发明之一。按性能的不同，电脑可分为巨型机、大型机、小型机、工作站和微型电脑等。'\n",
    "p_string = text.split('。')\n",
    "for line in p_string:\n",
    "    if re.match('电脑',line) is not None:\n",
    "        print(\"句子“\",line,\"”是以“电脑”开头的\")\n",
    "    elif line:\n",
    "        print(\"句子“\",line,\"”不是以“电脑”开头的\")\n",
    "    if re.search('电脑',line) is not None:\n",
    "        line = re.sub('电脑', '计算机',line)\n",
    "        print(\"将‘电脑’替换为‘计算机’后的结果: \",line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
